class_name MonsterDatabase extends Node

# 单例模式
static var instance = null

# 存储所有幻兽数据的字典
var monsters = {}

# 按类型分类的幻兽ID列表
var monster_types = {
    "普通": [],
    "稀有": [],
    "史诗": [],
    "传说": []
}

# 按元素分类的幻兽ID列表
var monster_elements = {
    "火": [],
    "水": [],
    "风": [],
    "地": [],
    "光": [],
    "暗": [],
    "无": []
}

# 初始化函数
func _init():
    if instance == null:
        instance = self
        _load_monster_data()
    else:
        print("警告: MonsterDatabase已经被实例化")

# 加载幻兽数据
func _load_monster_data():
    # 在实际项目中，这些数据会从JSON或其他数据文件加载
    # 这里为了演示，我们直接在代码中定义一些基础幻兽
    
    # 普通幻兽 - 火元素
    _add_monster({
        "id": "fire_slime",
        "name": "火焰史莱姆",
        "description": "一种生活在火山附近的小型幻兽，身体由半固态的火焰物质组成。",
        "type": "普通",
        "element": "火",
        "sprite_path": "res://assets/characters/monsters/common/fire_slime.png",
        "base_strength": 8,
        "base_agility": 5,
        "base_intelligence": 3,
        "base_endurance": 7,
        "base_spirit": 4,
        "strength_growth": 1.2,
        "agility_growth": 0.8,
        "intelligence_growth": 0.5,
        "endurance_growth": 1.0,
        "spirit_growth": 0.7,
        "initial_skills": ["fire_ball", "tackle"]
    })
    
    # 普通幻兽 - 水元素
    _add_monster({
        "id": "water_blob",
        "name": "水泡泡",
        "description": "栖息在湖泊和河流中的水元素幻兽，身体由纯净水组成，能自由改变形状。",
        "type": "普通",
        "element": "水",
        "sprite_path": "res://assets/characters/monsters/common/water_blob.png",
        "base_strength": 4,
        "base_agility": 7,
        "base_intelligence": 8,
        "base_endurance": 5,
        "base_spirit": 6,
        "strength_growth": 0.6,
        "agility_growth": 1.1,
        "intelligence_growth": 1.3,
        "endurance_growth": 0.8,
        "spirit_growth": 1.0,
        "initial_skills": ["water_jet", "bubble"]
    })
    
    # 稀有幻兽 - 风元素
    _add_monster({
        "id": "wind_falcon",
        "name": "风翼隼",
        "description": "高空飞行的风元素幻兽，翅膀能产生强大的气流，速度极快。",
        "type": "稀有",
        "element": "风",
        "sprite_path": "res://assets/characters/monsters/rare/wind_falcon.png",
        "base_strength": 7,
        "base_agility": 12,
        "base_intelligence": 8,
        "base_endurance": 5,
        "base_spirit": 9,
        "strength_growth": 1.0,
        "agility_growth": 1.8,
        "intelligence_growth": 1.2,
        "endurance_growth": 0.7,
        "spirit_growth": 1.3,
        "initial_skills": ["gust", "quick_attack", "wind_slash"]
    })
    
    # 稀有幻兽 - 地元素
    _add_monster({
        "id": "rock_golem",
        "name": "岩石魔像",
        "description": "由岩石和矿物质组成的地元素幻兽，防御力极高但行动缓慢。",
        "type": "稀有",
        "element": "地",
        "sprite_path": "res://assets/characters/monsters/rare/rock_golem.png",
        "base_strength": 11,
        "base_agility": 3,
        "base_intelligence": 5,
        "base_endurance": 14,
        "base_spirit": 7,
        "strength_growth": 1.6,
        "agility_growth": 0.4,
        "intelligence_growth": 0.7,
        "endurance_growth": 2.0,
        "spirit_growth": 1.0,
        "initial_skills": ["rock_throw", "harden", "earthquake"]
    })
    
    # 史诗幻兽 - 光元素
    _add_monster({
        "id": "light_unicorn",
        "name": "光辉独角兽",
        "description": "传说中的光元素幻兽，角上闪耀着纯净的光芒，能治愈伤口和净化黑暗。",
        "type": "史诗",
        "element": "光",
        "sprite_path": "res://assets/characters/monsters/epic/light_unicorn.png",
        "base_strength": 9,
        "base_agility": 11,
        "base_intelligence": 13,
        "base_endurance": 10,
        "base_spirit": 15,
        "strength_growth": 1.2,
        "agility_growth": 1.5,
        "intelligence_growth": 1.8,
        "endurance_growth": 1.3,
        "spirit_growth": 2.0,
        "initial_skills": ["healing_light", "purify", "light_beam", "holy_charge"]
    })
    
    # 史诗幻兽 - 暗元素
    _add_monster({
        "id": "shadow_wolf",
        "name": "暗影狼",
        "description": "栖息在黑暗森林中的暗元素幻兽，能融入阴影中移动，攻击迅猛。",
        "type": "史诗",
        "element": "暗",
        "sprite_path": "res://assets/characters/monsters/epic/shadow_wolf.png",
        "base_strength": 14,
        "base_agility": 13,
        "base_intelligence": 10,
        "base_endurance": 9,
        "base_spirit": 12,
        "strength_growth": 1.9,
        "agility_growth": 1.7,
        "intelligence_growth": 1.4,
        "endurance_growth": 1.2,
        "spirit_growth": 1.6,
        "initial_skills": ["shadow_bite", "dark_howl", "shadow_step", "nightmare"]
    })
    
    # 传说幻兽 - 多元素
    _add_monster({
        "id": "ancient_dragon",
        "name": "远古巨龙",
        "description": "传说中的幻兽之王，掌握多种元素力量，实力强大无比。",
        "type": "传说",
        "element": "无", # 多元素，这里用"无"表示
        "sprite_path": "res://assets/characters/monsters/legendary/ancient_dragon.png",
        "base_strength": 18,
        "base_agility": 15,
        "base_intelligence": 17,
        "base_endurance": 18,
        "base_spirit": 16,
        "strength_growth": 2.5,
        "agility_growth": 2.0,
        "intelligence_growth": 2.3,
        "endurance_growth": 2.5,
        "spirit_growth": 2.2,
        "initial_skills": ["dragon_breath", "ancient_roar", "elemental_fury", "dragon_claw", "dragon_scale"]
    })
    
    print("幻兽数据库加载完成，共加载 %d 种幻兽" % monsters.size())

# 添加幻兽数据到数据库
func _add_monster(monster_data):
    var id = monster_data.get("id", "")
    if id == "":
        print("错误: 幻兽数据缺少ID")
        return
    
    monsters[id] = monster_data
    
    # 按类型分类
    var monster_type = monster_data.get("type", "普通")
    if monster_types.has(monster_type):
        monster_types[monster_type].append(id)
    
    # 按元素分类
    var element = monster_data.get("element", "无")
    if monster_elements.has(element):
        monster_elements[element].append(id)

# 获取幻兽数据
func get_monster_data(monster_id):
    if monsters.has(monster_id):
        return monsters[monster_id]
    else:
        print("警告: 未找到ID为 %s 的幻兽数据" % monster_id)
        return null

# 创建幻兽实例
func create_monster(monster_id, level = 1):
    var monster_data = get_monster_data(monster_id)
    if monster_data:
        var monster = Monster.new(monster_data)
        # 如果指定的等级大于1，则升级到指定等级
        for i in range(1, level):
            monster.level_up()
        return monster
    else:
        return null

# 获取指定类型的幻兽列表
func get_monsters_by_type(monster_type):
    if monster_types.has(monster_type):
        return monster_types[monster_type]
    else:
        print("警告: 未找到类型为 %s 的幻兽" % monster_type)
        return []

# 获取指定元素的幻兽列表
func get_monsters_by_element(element):
    if monster_elements.has(element):
        return monster_elements[element]
    else:
        print("警告: 未找到元素为 %s 的幻兽" % element)
        return []

# 获取随机幻兽ID
func get_random_monster_id(monster_type = null, element = null):
    var available_monsters = []
    
    if monster_type != null and element != null:
        # 同时按类型和元素筛选
        for id in monsters.keys():
            var data = monsters[id]
            if data.get("type") == monster_type and data.get("element") == element:
                available_monsters.append(id)
    elif monster_type != null:
        # 只按类型筛选
        available_monsters = get_monsters_by_type(monster_type)
    elif element != null:
        # 只按元素筛选
        available_monsters = get_monsters_by_element(element)
    else:
        # 不筛选，使用所有幻兽
        available_monsters = monsters.keys()
    
    if available_monsters.size() > 0:
        var random_index = randi() % available_monsters.size()
        return available_monsters[random_index]
    else:
        print("警告: 未找到符合条件的幻兽")
        return null

# 获取随机幻兽实例
func get_random_monster(monster_type = null, element = null, min_level = 1, max_level = 5):
    var monster_id = get_random_monster_id(monster_type, element)
    if monster_id:
        var level = min_level
        if max_level > min_level:
            level = min_level + randi() % (max_level - min_level + 1)
        return create_monster(monster_id, level)
    else:
        return null
